//
//  SegmentViewController.m
//  WYProjectNavGation
//
//  Created by dihuijun on 17/4/26.
//  Copyright © 2017年 Cycle. All rights reserved.
//

#import "SegmentViewController.h"
#import "ScienceViewController.h"
#import "SocietyViewController.h"
#import "TopLineViewController.h"
#import "VideoViewController.h"
#import "ReaderViewController.h"
#import "HotViewController.h"

#define screenW [UIScreen mainScreen].bounds.size.width
#define screenH [UIScreen mainScreen].bounds.size.height
//label宽度
static CGFloat const lblWidth  = 100.0;
static CGFloat const lblHeight = 44.0;
static CGFloat const radio     = 1.2;

@interface SegmentViewController ()<UIScrollViewDelegate>

/**   titlescrollView 标题的scrollView  */
@property (nonatomic, strong) UIScrollView *titleScrollView;
/**   titlescrollView 内容的scrollView  */
@property (nonatomic, strong) UIScrollView *bodyScrollView;
/**   selectedLabel记录按钮点击状态   */
@property (nonatomic, weak)  UILabel *selectedLaebl;
/**   储存label   */
@property (nonatomic, strong) NSMutableArray *labelArray;
@end

@implementation SegmentViewController

/**   titlescrollView   */
- (UIScrollView *)titleScrollView{
    if(!_titleScrollView){
        _titleScrollView = [[UIScrollView  alloc]initWithFrame:CGRectMake(0, 24, screenW, 40)];
        
    }
    return _titleScrollView;
}
/**   bodyscrollView   */
- (UIScrollView *)bodyScrollView{
    if(!_bodyScrollView){
        _bodyScrollView = [[UIScrollView  alloc]initWithFrame:CGRectMake(0, 0, screenW, screenH)];
        
    }
    return _bodyScrollView;
}

/**   储存label   */
- (NSMutableArray *)labelArray{
    if(!_labelArray){
        _labelArray = [NSMutableArray array];
    }
    return _labelArray;
}





- (void)viewDidLoad {
    [super viewDidLoad];
    //1.set up Navgation
    [self setupNav];
    //2.创建子控制器
    [self setupChildViewControllers];
    //3.set up titleScrollView
    [self setupScrollView];
    






}
#pragma mark --- 1.setupNav
- (void)setupNav
{
    self.view.backgroundColor = [UIColor redColor];
    
    
}

#pragma mark --- 2.setupChildViewControllers
- (void)setupChildViewControllers
{
    
    //头条
    TopLineViewController *topLineVC = [[TopLineViewController alloc]init];
    topLineVC.title                  = @"头条";
    [self addChildViewController:topLineVC];
    
    
    //热点
    HotViewController *HotVC         = [[HotViewController alloc]init];
    HotVC.title                      = @"热点";
    [self addChildViewController:HotVC];
    
    
    //视屏
    VideoViewController *VideoVC     = [[VideoViewController alloc]init];
    VideoVC.title                    = @"视屏";
    [self addChildViewController:VideoVC];
    
    
    //社会
    SocietyViewController *socityVC  = [[SocietyViewController alloc]init];
    socityVC.title                   = @"社会";
    [self addChildViewController:socityVC];
    
    
    //阅读
    ReaderViewController *ReaderVC   = [[ReaderViewController alloc]init];
    ReaderVC.title                   = @"阅读";
    [self addChildViewController:ReaderVC];
    
    
    //科技
    ScienceViewController *scienceVC = [[ScienceViewController alloc]init];
    scienceVC.title                  = @"科技";
    [self addChildViewController:scienceVC];
}



#pragma mark --- 3.setupTitleScrollView
- (void)setupScrollView
{
    
    /*********   一： titleScrollView  **********/
    NSUInteger titlecount = self.childViewControllers.count;
    CGFloat lblX = 0;
    CGFloat lblY = 0;
    
    for (NSInteger i = 0; i < titlecount; i++) {
        UIViewController *vc     = self.childViewControllers[i];
        //3.1 创建label
        UILabel *lbl             = [UILabel new];
        //添加到数组
        [self.labelArray addObject:lbl];
        //3.2 设置title和tag
        lbl.text                 = vc.title;
        lbl.tag                  = i;
        lbl.textAlignment        = NSTextAlignmentCenter;
        lbl.highlightedTextColor = [UIColor redColor]; ///高亮的颜色
        //3.3 设置frame
        lblX                     = i*lblWidth;
        lbl.frame                = CGRectMake(lblX, lblY, lblWidth, lblHeight);
        //3.4 添加手势
        lbl.userInteractionEnabled  = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TitleClick:)];
        [lbl addGestureRecognizer:tap];
        //开始默认选中第一个
        if (i==0) {
            [self TitleClick:tap];
        }
        
        //3.5 设置高亮文字
        lbl.highlightedTextColor    = [UIColor redColor];
        
        //3.6 添加到scrollView
        [self.titleScrollView addSubview:lbl];
    }

    ///导航栏的scroll
    self.navigationItem.titleView = self.titleScrollView;
    self.titleScrollView.contentSize = CGSizeMake(lblWidth*titlecount, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
//    self.titleScrollView.bounces = NO;
    
    /*********    / titleScrollView  **********/
    
    
    
    
    /*********   二： bodyScrollView  **********/
    [self.view addSubview:self.bodyScrollView];
    self.bodyScrollView.contentSize = CGSizeMake(titlecount*screenW, 0);
    self.bodyScrollView.pagingEnabled = YES;//分页
    self.bodyScrollView.bounces = NO;
    self.bodyScrollView.showsHorizontalScrollIndicator = NO;
    self.bodyScrollView.delegate = self;
    /*********    /bodyScrollView  **********/
 
    
}
/**  3.4 点击label  */
- (void)TitleClick:(UITapGestureRecognizer *)tap{
    NSLog(@"...");
    
    UILabel *selectedLbl = (UILabel *)tap.view;
    //3.4.1 label选中状态
    [self selectedTheLabel:selectedLbl];
    
    //3.4.2 “titleScrollView的偏移量” 并 “添加控制器”
    
    NSInteger index = selectedLbl.tag;
    
    CGFloat offsetX = index*screenW;

    [self showVC:index offSetX:offsetX];
    
    //3.4.3 点击选中之后让label居中显示
    [self centerSelectedLabel:selectedLbl];
    
    
    
    
}
/**   3.4.1 label选中状态   */
- (void)selectedTheLabel:(UILabel *)selLabel{
    //取消上一次的选中状态
    _selectedLaebl.highlighted = NO;
    //还原形态
    _selectedLaebl.transform = CGAffineTransformIdentity;
    //还原颜色
    _selectedLaebl.textColor = [UIColor blackColor];
    
    
    //高亮
    selLabel.highlighted = YES;
    //第一次点击是改变了形态的
    selLabel.transform = CGAffineTransformMakeScale(radio, radio);
    
    //记录
    _selectedLaebl = selLabel;
}
/** 3.4.2 显示控制器*/
- (void)showVC:(NSInteger)index  offSetX:(CGFloat)offsetX{
    
    //偏移
    self.bodyScrollView.contentOffset = CGPointMake(offsetX, 0);
    //取出控制器
    UIViewController *vc = self.childViewControllers[index];
    
    //如果已经加载过就不需要在加载
    if (vc.isViewLoaded)return;
    
    //没有加载过就加载
    vc.view.frame = CGRectMake(offsetX, 0, self.bodyScrollView.bounds.size.width, self.bodyScrollView.bounds.size.height);
    
    [self.bodyScrollView addSubview:vc.view];
    
}

#pragma mark ---- 3.bodyScrollViewDelegate
/** 滚动完成切换控制器和改变标题选中状态*/
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    //1.得到页数
    NSInteger page = scrollView.contentOffset.x / scrollView.bounds.size.width;
    
    //2.显示控制器
    CGFloat offsetX = page*screenW;
    [self showVC:page offSetX:offsetX];
    //3.切换label
    UILabel *selectLaebl = self.labelArray[page];
    [self selectedTheLabel:selectLaebl];
    
    //4.点击选中之后让label居中显示
    [self centerSelectedLabel:selectLaebl];
    
    
}
/***   滚动的时候改变字的颜色和字体大小 */
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //1.得到页数
    CGFloat page = scrollView.contentOffset.x / scrollView.bounds.size.width;
    //2.1 初始的index
    NSInteger leftIndex = page;
    //2.2 滑动到第二个的index
    NSInteger rightIndex = leftIndex+1;
    
    //3.1 取出第一个index里面的label
    UILabel *leftLabel = self.labelArray[leftIndex];
    
    //3.2 取出第二个index的label(可能越界，需要判断)
    
    UILabel *rightLabel;
    if (rightIndex < self.labelArray.count-1) {
        rightLabel = self.labelArray[rightIndex];
    }
    
    
    //4.计算缩放比例
    CGFloat rightScale = page-leftIndex;
    CGFloat leftScale  = 1-rightScale;
    //5.开始缩放
    leftLabel.transform = CGAffineTransformMakeScale(leftScale*0.2+1, leftScale*0.2+1);
    rightLabel.transform = CGAffineTransformMakeScale(rightScale*0.2+1, rightScale*0.2+1);
    
    //6.改变颜色
    leftLabel.textColor = [UIColor colorWithRed:leftScale green:0 blue:0 alpha:1];
    rightLabel.textColor = [UIColor colorWithRed:rightScale green:0 blue:0 alpha:1];
    
    
    
}




/** 3.4.3 居中显示label*/
- (void)centerSelectedLabel:(UILabel *)selectedLabel
{
    //偏移的距离
    CGFloat offsetX = selectedLabel.center.x-screenW*0.5;
    if (offsetX<0) offsetX = 0;
    //最大偏移
    CGFloat maxOffsetX = self.titleScrollView.contentSize.width-screenW;
    if (offsetX>maxOffsetX) offsetX = maxOffsetX;
    //设置偏移
    [self.titleScrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}






@end
